﻿using System;
using System.Linq;
using System.Windows;
using CCWP7.Model;
using Caliburn.Micro;
using Microsoft.Phone.Shell;
using CCWP7.STA;

namespace CCWP7.CaliburnMicro
{
    public class ProjectPageViewModel : Screen
    {
        IProjectReport _projectReport;
        readonly IServerDataManager _serverDataManager;
        readonly INavigationService _navigationService;
        readonly IShellTileDataService _shellTileDataService;

        public ProjectPageViewModel(IServerDataManager serverDataManager,
        INavigationService navigationService,
        IShellTileDataService shellTileDataService)
        {
            _shellTileDataService = shellTileDataService;
            _navigationService = navigationService;
            _serverDataManager = serverDataManager;
        }

        string _projectId;
        public string ProjectId
        {
            private get
            {
                return _projectId;
            }
            set
            {
                _projectId = value;

                var id = int.Parse(value);

                var query = from s in _serverDataManager.Servers
                            where s.Report != null
                            from p in s.Report.Projects
                            where p.GetHashCode() == id
                            select p;

                SetProject(query.Single());
            }
        }

        void SetProject(IProjectReport projectReport)
        {
            _projectReport = projectReport;

            Activity = projectReport.Activity;
            BuildStage = projectReport.BuildStage;
            Category = projectReport.Category;
            LastBuildLabel = projectReport.LastBuildLabel;
            LastBuildStatus = projectReport.LastBuildStatus;
            LastBuildTime = projectReport.LastBuildTime;
            Name = projectReport.Name;
            NextBuildTime = projectReport.NextBuildTime;
            ServerName = projectReport.ServerName;
            Status = projectReport.Status;
            WebUrl = projectReport.WebUrl;

            CanPin = !(CanUnPin = CheckIsPinned(projectReport));

            Refresh();
        }

        bool CheckIsPinned(IProjectReport projectReport)
        {
            ShellTile tile = GetTile(projectReport);

            return tile != null;
        }

        ShellTile GetTile(IProjectReport projectReport)
        {
            Uri uri = GetNavigationUri(projectReport);

            var tile = ShellTile.ActiveTiles.FirstOrDefault(st => st.NavigationUri == uri);

            return tile;
        }

        Uri GetNavigationUri(IProjectReport projectReport)
        {
            return new Uri(string.Format("/ProjectPage.xaml?ProjectId={0}", ProjectId), UriKind.Relative);
        }

        public string PageTitle
        {
            get
            {
                return Name;
            }
        }

        public string Activity { get; private set; }
        public string BuildStage { get; private set; }
        public string Category { get; private set; }
        public string LastBuildLabel { get; private set; }
        public string LastBuildStatus { get; private set; }
        public DateTime LastBuildTime { get; private set; }
        public string Name { get; private set; }
        public DateTime NextBuildTime { get; private set; }
        public string ServerName { get; private set; }
        public string Status { get; private set; }
        public string WebUrl { get; private set; }

        protected override void OnViewReady(object view)
        {
            base.OnViewReady(view);

            Refresh();
        }

        public bool CanPin { get; private set; }

        public void Pin()
        {
            var uri = GetNavigationUri(_projectReport);

            var data = _shellTileDataService.GetShellTile(_projectReport);

            ShellTile.Create(uri, data);
        }

        public bool CanUnPin { get; private set; }

        public void UnPin()
        {
            var tile = GetTile(_projectReport);

            tile.Delete();

            MessageBox.Show("Project Tile Un-pinned");
        }
    }
}

